<html>
    <head>
        <title>编辑</title>
        <link rel="stylesheet" href="/js/layui/css/layui.css">
        <!-- 引入样式 -->
        <script type="text/javascript" src="/js/jquery-1.11.0.min.js"></script>
        <script src="/js/layui/layui.js"></script>
        <style>
            .layui-input-block{
                margin-left:0px !important;
            }
            .layui-form-item{
                width:90%;
                margin:15px auto;
            }
        </style>
    </head>
    <body>
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input id="title" type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <select name="apk" id="apk" lay-filter="selectApk" lay-verify="required">
                        <option value="">选择应用</option>
                    </select>
                </div>
            </div>
            <div style="display:none;" id="subpage" class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" id="sub_btn" class="layui-btn layui-btn-sm" lay-filter="subpage">二级界面</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">设置默认</label>
                <div class="layui-input-block">
                    <input id="default" type="checkbox" name="default" lay-skin="switch">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </body>
    <script>
        function getQueryVariable(variable)
        {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        }

        //Demo
        layui.use('form', function(){
            var form = layui.form;
            var layer = layui.layer ;

            $("#title").val(decodeURIComponent(getQueryVariable("name")))
            if(getQueryVariable("default") == "true")
                $("#default").attr("checked" , "checked")
            form.render()

            //监听提交
            form.on('submit(formDemo)', function(data){
                console.log(data)
                // console.log(parent.vue.$data.TVscreenInfo.panel.MAIN.data[parent.vue.$data.TVscreenInfo.panel.MAIN.item].subMenu)
                parent.vue.$data.TVscreenInfo.panel.MAIN.data[parent.vue.$data.TVscreenInfo.panel.MAIN.item].subMenu[getQueryVariable("index")*1].name = data.field.title
                parent.vue.$data.TVscreenInfo.panel.MAIN.data[parent.vue.$data.TVscreenInfo.panel.MAIN.item].subMenu[getQueryVariable("index")*1].apk = data.field.apk.split("\|")[0]
                parent.vue.$data.TVscreenInfo.panel.MAIN.data[parent.vue.$data.TVscreenInfo.panel.MAIN.item].subMenu[getQueryVariable("index")*1].apkurl = data.field.apk.split("\|")[1]
                for(var i in parent.vue.$data.TVscreenInfo.panel.MAIN.data[parent.vue.$data.TVscreenInfo.panel.MAIN.item].subMenu) {
                    parent.vue.$data.TVscreenInfo.panel.MAIN.data[parent.vue.$data.TVscreenInfo.panel.MAIN.item].subMenu[i].default = false
                }
                parent.vue.$data.TVscreenInfo.panel.MAIN.data[parent.vue.$data.TVscreenInfo.panel.MAIN.item].subMenu[getQueryVariable("index")*1].default = (data.field.default != undefined ? true : false)
                parent.layer.closeAll()
                return false;
            });

            function render_select(data){
                for(var i in data) {
                    $("#apk").append(new Option(data[i].apkName , data[i].packageName + "|" + data[i].packageUrl))
                    if(data[i].packageName == decodeURIComponent(getQueryVariable("apk"))) {
                        // subpage
                        if(data[i].packageName.indexOf("com.desktop.ppt") != -1) {
                            $("#subpage").css("display" , "block")
                        }
                        $("#apk").val(data[i].packageName + "|" + data[i].packageUrl)
                    }
                }
                form.render("select")
            }

            if(parent.vue.$data.apks.length == 0) {
                $.ajax({
                    url: parent.global.apks ,
                    success:function(res){
                        if(res.code == 200) {
                            render_select(res.data)
                        }
                    }
                })
            }else{
                render_select(parent.vue.$data.apks)
            }

            form.on("select(selectApk)" , function(obj){

                if(obj.value.indexOf("com.desktop.ppt") != -1) {
                    $("#subpage").css("display" , "block")
                    var width = parent.vue.$data.TVscreenInfo.width
                    var height = parent.vue.$data.TVscreenInfo.height
                    parent.layer.open({
                        type: 2,
                        shade: false,
                        area: [width , height],
                        maxmin: true,
                        content: './goSubPage?type=submenu&index='+getQueryVariable("index"),
                        zIndex: layer.zIndex, //重点1
                        success: function(layero){
                            layer.setTop(layero); //重点2
                        }
                    });
                }else{
                    $("#subpage").css("display" , "none")
                }
            })

            $("#sub_btn").click(function(res){
                var width = parent.vue.$data.TVscreenInfo.width
                var height = parent.vue.$data.TVscreenInfo.height
                parent.layer.open({
                    type: 2,
                    shade: false,
                    area: [width , height],
                    maxmin: true,
                    content: './goSubPage?type=submenu&index='+getQueryVariable("index"),
                    zIndex: layer.zIndex, //重点1
                    success: function(layero){
                        layer.setTop(layero); //重点2
                    }
                });
            })
        });

    </script>
</html>